package com.huiminpay.merchant.common.intercept;

/**
 * @Author: yz
 * @Date: 2024/7/19 18:34
 */

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.domain.RestErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class GlobalExceptionHandler {
    // 异常处理分类处理
    @ExceptionHandler
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public RestErrorResponse processException(HttpServletRequest request,
                                              HttpServletResponse response,
                                              Exception ex) {
        if (ex instanceof BusinessException) { //自定义异常
            BusinessException ee = (BusinessException) ex;
            return new RestErrorResponse(String.valueOf(ee.getErrorCode().getCode()), ee.getErrorCode().getDesc());
        }
        //未知异常
        return new RestErrorResponse(String.valueOf(CommonErrorCode.UNKNOWN.getCode()),
                CommonErrorCode.UNKNOWN.getDesc());
    }


}
